package entity

import (
	"errors"
	"fmt"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

//Role 角色管理
type Role struct {
	ID             int    `gorm:"AUTO_INCREMENT;primary_key;column:RoleId"`
	Name           string `gorm:"column:RoleName;size:20"`
	RoleDes        string `gorm:"column:RoleDes;size:30"`
	CanIdno        int    `gorm:"column:CanIdno"`
	CanIndoCaption string `gorm:"-"`
	SortNo         int    `gorm:"column:SortNo"`
}

//TableName Role 角色表名
func (Role) TableName() string {
	return "t_Role"
}
func (o *Role) AfterFind(tx *gorm.DB) (err error) {
	if o.CanIdno == 1 {
		o.CanIndoCaption = "可见"
	} else {
		o.CanIndoCaption = "隐藏"
	}
	return nil
}
func (o *Role) BeforeCreate(tx *gorm.DB) (err error) {
	var count int64
	err = tx.Model(Role{}).Where(&Role{Name: o.Name}).Count(&count).Error
	if err != nil {
		return err
	}
	if count > 0 {
		info := fmt.Sprintf("角色名称 '%s' 已存在，请修改！", o.Name)
		return errors.New(info)
	}
	return
}
func RoleEF() base.EntityFunc {
	return func() interface{} {
		entity := Role{}
		return &entity
	}
}
func RoleESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []Role
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("Role", base.NewDefaultDao(RoleEF(), RoleESF(), nil, "my"))
}
